type Fish = { swim: () => void };
type Bird = { fly: () => void };

function isFish(pet: Fish | Bird): pet is Fish {
    return (pet as Fish).swim !== undefined;
}

let pet!: Fish | Bird;
 
if (isFish(pet)) {
  pet.swim();
} else {
  pet.fly();
}